import json
import threading
import time

import requests as requests
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.keys import Keys

from config import webdriverPath, hostUrl
from wos.test import parse_html


# pip install
# import PyExecJS
class WOS_spider:

    def __init__(self):
        self.listEnd = False
        self.list = []
        self.citationList = []

    def run(self):
        # url_root = 'https://webofscience-clarivate-cn-s.era.lib.swjtu.edu.cn/wos/alldb/basic-search'
        # 创建ChromeOptions对象
        chrome_options = webdriver.ChromeOptions()
        # 禁止加载图片等资源
        chrome_options.add_argument("--disable-images")
        chrome_options.add_argument("--disable-plugins")
        chrome_options.add_argument("--disable-extensions")
        # 设置全屏参数
        chrome_options.add_argument("--start-maximized")

        # 创建WebDriver对象时传入ChromeOptions
        self.driver = webdriver.Chrome(options=chrome_options, service=Service(webdriverPath))
        self.driver.get('http://www.scihuber.com/e/member/login/')  # 打开的页面

        # # 登录
        # WebDriverWait(driver, wait_time).until(
        #     EC.visibility_of_element_located((By.ID, 'username'))
        # )
        # self.driver.find_element(By.ID, 'username').send_keys('056958')
        # self.driver.find_element(By.ID, 'password').send_keys('13653800549')
        self.driver.find_element(By.ID, 'username').send_keys('917612')
        self.driver.find_element(By.ID, 'password').send_keys('02690773584')
        self.driver.find_element(By.CSS_SELECTOR, '#maincolumn .lBtn').click()
        # input("请手动操作至论文详情页面,完成后按Enter键继续...")
        time.sleep(3)
        self.driver.get('http://www.scihuber.com/e/action/ShowInfo.php?classid=186&id=3324')  # 打开的页面
        time.sleep(1)

    def closeCookie(self):

        # 关闭管理Cookie栏
        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CSS_SELECTOR, '#onetrust-close-btn-container > button'))
        )
        time.sleep(5)
        self.driver.find_element(By.CSS_SELECTOR, '#onetrust-close-btn-container > button').click()

        WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located((By.CLASS_NAME, 'selects'))
        )

    def readItem(self, item):
        res = requests.post(hostUrl + '/literature/getByAccessionNo',
                            json={'accessionNo': item['accessionNo']})
        if res.json().get('data') == '0':
            print(item['dataSource'],item['title'], "保存")
            self.driver.get(item['url'])
            # 等待页面加载
            # 或者等待直到某个元素可见
            WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located((By.CSS_SELECTOR,
                                                  'app-page-controls .pagination > div > button:nth-child(4)'))
            )
            time.sleep(1)
            # 解析HTML
            html = self.driver.page_source
            index, data = parse_html(html)
            # 类型来源
            data['type'] = 10
            data['sourceWebsite'] = 'wos'
            data['language'] = 'en'
            data['datasource'] = item['dataSource']
            data['database'] = '期刊'
            # data['infoData'] = item['infoData']
            # 获取当前url
            data['url'] = item['url']

            requests.post(hostUrl + '/literature/wosSave', json=data)
        else:
            print(item['dataSource'], "该论文已存在")


def exec_Wos(arr):
    isError = True
    index = -1
    while isError:
        isError = False
        a = None
        try:
            # a.closeCookie()
            for i in arr:
                if index > arr.index(i):
                    continue
                if a is None:
                    a = WOS_spider()
                    a.run()
                index =arr.index(i)
                a.readItem(i)
            if a is not None:
                a.driver.quit()
        except Exception as e:
            print(e)
            isError = True
            index = index-1
            a.driver.quit()
            time.sleep(10)

def arrThread():

    isError = True
    index = -1
    while isError:
        res = requests.get('http://localhost:1728/qikan/wosTemp/getWosTemp')
        if res.json()['success']:
            exec_Wos(res.json()['data'])
        else:
            isError = False




#   获取明细数据
if __name__ == "__main__":
    # res = requests.get('http://localhost:1728/qikan/wosTemp/setPage?initialValue=0')
    for i in range(0, 1):
        thread = threading.Thread(target=arrThread)
        # threads.append(thread)
        thread.start()  # 启动线程
        time.sleep(20)